package com.gnerv.sylvanas.business.manage.logic;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.framework.core.BaseUkIdGenerate;
import com.gnerv.sylvanas.business.manage.model.bo.RoleBO;
import com.gnerv.sylvanas.business.manage.model.dto.RoleAccountDTO;
import com.gnerv.sylvanas.business.manage.model.dto.RoleApiDTO;
import com.gnerv.sylvanas.business.manage.model.dto.RoleCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.RoleDTO;
import com.gnerv.sylvanas.business.manage.model.dto.RoleUpdateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.RoleViewDTO;
import com.gnerv.sylvanas.business.manage.operation.RoleOperationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class RoleLogicService  {

    private final RoleOperationService roleOperationService;
    private final BaseUkIdGenerate baseUkIdGenerate;

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(RoleCreateDTO roleCreateDTO) {
        RoleBO roleBO = roleCreateDTO.toRoleBO();
        roleBO.setUkId(baseUkIdGenerate.generate());
        roleOperationService.create(roleBO);
        return RestResult.ok();
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        roleOperationService.deleteByUkId(ukId);
        roleOperationService.unbindAccount(ukId);
        roleOperationService.unbindApi(ukId);
        roleOperationService.unbindView(ukId);
        return RestResult.ok();
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(RoleUpdateDTO roleUpdateDTO) {
        RoleBO roleBO = roleUpdateDTO.toRoleBO();
        roleOperationService.updateByUkId(roleBO);
        return RestResult.ok();
    }

    
    @Transactional(readOnly = true)
    public RestResult<RoleDTO> selectByUkId(String ukId) {
        RoleDTO roleDTO = roleOperationService.selectByUkId(ukId);
        return RestResult.ok(roleDTO);
    }

    
    @Transactional(readOnly = true)
    public RestResult<List<RoleDTO>> selectListByName(String name) {
        return RestResult.ok(roleOperationService.selectListByName(name));
    }

    
    public RestResult<Void> bindAccount(RoleAccountDTO roleAccountDTO) {
        roleOperationService.unbindAccount(roleAccountDTO.getRoleUkId());
        roleOperationService.bindAccount(roleAccountDTO.getRoleUkId(), roleAccountDTO.getAccountUkIdList());
        return RestResult.ok();
    }

    
    public RestResult<Void> bindApi(RoleApiDTO roleApiDTO) {
        roleOperationService.unbindApi(roleApiDTO.getRoleUkId());
        roleOperationService.bindApi(roleApiDTO.getRoleUkId(), roleApiDTO.getApiUkIdList());
        return RestResult.ok();
    }

    
    public RestResult<Void> bindView(RoleViewDTO roleViewDTO) {
        roleOperationService.unbindView(roleViewDTO.getRoleUkId());
        roleOperationService.bindView(roleViewDTO.getRoleUkId(), roleViewDTO.getViewUkIdList());
        return RestResult.ok();
    }

}
